package com.dfcj.oauth2server.service;

import com.dfcj.oauth2server.dao.AccountUserMapper;
import com.dfcj.oauth2server.entity.AccountUser;
import com.dfcj.oauth2server.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyUserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
    @Autowired
    private AccountUserMapper accountUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
        //return accountUserMapper.selectByUsername(username);
        UserDetails userDetails=accountUserMapper.selectByUsername(username);

        List<Role> roles=new ArrayList<>();
        Role role=new Role();
        role.setRoleId("001");
        role.setRoleName("Role_Admin");
        roles.add(role);
        ((AccountUser) userDetails).setAuthorities(roles);

        return userDetails;
        //return accountUserMapper.selectByPrimaryKey("2");
    }
}
